package com.manager.plat.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.manager.plat.dao.IConfigRouterDao;
import com.manager.plat.dao.IConfigBusinessDao;
import com.manager.plat.dao.IConfigLowerPartnerDao;
import com.manager.plat.vo.ConfigRouterVo;
import com.manager.plat.vo.ConfigBusinessVo;
import com.manager.plat.vo.ConfigLowerPartnerVo;
import com.manager.plat.vo.UserInfoVo;
import com.opensymphony.xwork2.ActionSupport;

public class ConfigRouterAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2587636782201781391L;

	private Logger logger = Logger.getLogger(ConfigRouterAction.class);
	
	private IConfigRouterDao configRouterDao;					//渠道路由信息数据接口
	private IConfigLowerPartnerDao configLowerPartnerDao;		//渠道信息数据接口
	private IConfigBusinessDao configBusinessDao;				//业务配置信息数据接口
	private List<ConfigRouterVo> configRouterVoList;			//渠道路由信息列表
	private List<ConfigLowerPartnerVo> configLowerPartnerVoList;//渠道信息列表
	private List<ConfigBusinessVo> configBusinessVoList;		//业务配置信息列表
	private ConfigRouterVo configRouterVo;						//渠道路由信息VO
	private String actionPath;									//操作成功跳转路径
	
	public String getConfigRouterList(){
		configRouterVoList = configRouterDao.getConfigRouterList();
		configLowerPartnerVoList = configLowerPartnerDao.getConfigLowerPartnerList();
		return SUCCESS;
	}
	
	public String addConfigRouter(){
		configRouterVo = null;
		configLowerPartnerVoList = configLowerPartnerDao.getConfigLowerPartnerList();
		configBusinessVoList = configBusinessDao.getConfigBusinessList();
		return SUCCESS;
	}
	
	public String editConfigRouter(){
		if(configRouterVo.getId() != 0){
			configRouterVo = configRouterDao.getConfigRouterById(configRouterVo.getId());
		}
		
		configLowerPartnerVoList = configLowerPartnerDao.getConfigLowerPartnerList();
		configBusinessVoList = configBusinessDao.getConfigBusinessList();
		return SUCCESS;
	}
	
	public String updateConfigRouter(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		if(configRouterVo.getId() == 0){
			configRouterVo.setAdd_user(userInfoVo.getId());
			int count = configRouterDao.insertConfigRouter(configRouterVo);
			if(count > 0){
				logger.info(String.format("[%s]添加[%s]渠道路由信息成功", userInfoVo.getLogin_username(),configRouterVo.getSp_number()));
			}else{
				logger.info(String.format("[%s]添加[%s]渠道路由信息失败", userInfoVo.getLogin_username(),configRouterVo.getSp_number()));
			}
			
		}else {
			configRouterVo.setUpdate_user(userInfoVo.getId());
			int count = configRouterDao.updateConfigRouter(configRouterVo);
			if(count > 0){
				logger.info(String.format("[%s]更新[%s]渠道路由信息成功", userInfoVo.getLogin_username(),configRouterVo.getSp_number()));
			}else{
				logger.info(String.format("[%s]更新[%s]渠道路由信息失败", userInfoVo.getLogin_username(),configRouterVo.getSp_number()));
			}
		}
		
		actionPath = "configRouter/getConfigRouterList.action";
		return SUCCESS;
	}
	
	public String deleteConfigRouter(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		
		int count = configRouterDao.deleteConfigRouter(configRouterVo.getId(), userInfoVo.getId());
		if(count > 0){
			logger.info(String.format("[%s]删除[%s]渠道路由信息成功", userInfoVo.getLogin_username(),configRouterVo.getSp_number()));
		}else{
			logger.info(String.format("[%s]删除[%s]渠道路由信息失败", userInfoVo.getLogin_username(),configRouterVo.getSp_number()));
		}
		actionPath = "configRouter/getConfigRouterList.action";
		return SUCCESS;
	}
	
	
	public IConfigRouterDao getConfigRouterDao() {
		return configRouterDao;
	}
	public void setConfigRouterDao(IConfigRouterDao configRouterDao) {
		this.configRouterDao = configRouterDao;
	}
	public IConfigLowerPartnerDao getConfigLowerPartnerDao() {
		return configLowerPartnerDao;
	}
	public void setConfigLowerPartnerDao(IConfigLowerPartnerDao configLowerPartnerDao) {
		this.configLowerPartnerDao = configLowerPartnerDao;
	}
	public IConfigBusinessDao getConfigBusinessDao() {
		return configBusinessDao;
	}
	public void setConfigBusinessDao(IConfigBusinessDao configBusinessDao) {
		this.configBusinessDao = configBusinessDao;
	}
	public List<ConfigRouterVo> getConfigRouterVoList() {
		return configRouterVoList;
	}
	public void setConfigRouterVoList(List<ConfigRouterVo> configRouterVoList) {
		this.configRouterVoList = configRouterVoList;
	}
	public List<ConfigLowerPartnerVo> getConfigLowerPartnerVoList() {
		return configLowerPartnerVoList;
	}
	public void setConfigLowerPartnerVoList(List<ConfigLowerPartnerVo> configLowerPartnerVoList) {
		this.configLowerPartnerVoList = configLowerPartnerVoList;
	}
	public List<ConfigBusinessVo> getConfigBusinessVoList() {
		return configBusinessVoList;
	}
	public void setConfigBusinessVoList(List<ConfigBusinessVo> configBusinessVoList) {
		this.configBusinessVoList = configBusinessVoList;
	}
	public ConfigRouterVo getConfigRouterVo() {
		return configRouterVo;
	}
	public void setConfigRouterVo(ConfigRouterVo configRouterVo) {
		this.configRouterVo = configRouterVo;
	}
	public String getActionPath() {
		return actionPath;
	}
	public void setActionPath(String actionPath) {
		this.actionPath = actionPath;
	}
}